﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class BookingSlot : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsSlots.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsSlots_Select);
        dsSlots.Insert += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsSlots_Save);
        dsSlots.Update += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsSlots_Save);
        dsSlots.Delete += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsSlots_Save);
        btnAdd.Click += new EventHandler(btnAdd_Click);
        this.gridSlots.ItemDataBound += new GridItemEventHandler(gridSlots_ItemDataBound);
    }

    void gridSlots_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.Edit && !(e.Item is GridDataInsertItem))
        {
            RadDateTimePicker dtTime = e.Item.FindControl("dtTime") as RadDateTimePicker;
            BrandUnion.CustomData.BookingSlot slot = e.Item.DataItem as BrandUnion.CustomData.BookingSlot;
            if (dtTime != null && slot != null)
            {
                dtTime.SelectedDate = slot.DateTime;
            }
        }
    }

    void dsSlots_Save(object sender, Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs e)
    {
        BrandUnion.CustomData.BookingSlotFactory factory = new BrandUnion.CustomData.BookingSlotFactory();
        BrandUnion.CustomData.BookingSlot slot = null;

        if (e.Operation == Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs.Operations.Insert)
        {
            slot = new BrandUnion.CustomData.BookingSlot();
        }
        else
        {
            int id = (int)e.Keys["Id"];
            var cid = (System.Data.Linq.Binary)e.Keys["CID"];
            slot = factory.GetById(id);
            if (!slot.CID.Equals(cid))
            {
                throw new Acms.Exceptions.ConcurencyException();
            }
        }

        if (slot != null && e.Operation == Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs.Operations.Delete)
        {
            factory.Delete(slot);
        }
        else
        {
            //DateTime control
            RadDateTimePicker dtTime = null;// gridSlots.MasterTableView.
            if (e.Operation == Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs.Operations.Insert)
            {
                dtTime = gridSlots.MasterTableView.GetInsertItem().FindControl("dtTime") as RadDateTimePicker;
            }
            else
            {
                dtTime = gridSlots.MasterTableView.Items[gridSlots.EditIndexes[0]].FindControl("dtTime") as RadDateTimePicker;
            }

            if (dtTime != null && dtTime.SelectedDate != null)
            {
                slot.DateTime = dtTime.SelectedDate.Value;
            }

            slot.NoSlots = int.Parse((string)e.NewValues["NoSlots"]);
            factory.Save(slot);
        }
    }

    void btnAdd_Click(object sender, EventArgs e)
    {
        gridSlots.MasterTableView.IsItemInserted = true;
        gridSlots.MasterTableView.Rebind();
    }

    void dsSlots_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(new BrandUnion.CustomData.BookingSlotFactory().GetAll());
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }


}
